package demo1.servlet.userMai;

import com.alibaba.fastjson.JSON;
import demo1.pojo.goods;
import demo1.pojo.gouwucar;
import demo1.service.goodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/showGoodsNum")
public class showGoodsNum extends HttpServlet {//展示商品库存
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//删除商品
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset = utf-8");
        //接受数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为goods对象
        int goodsID = JSON.parseObject(params, int.class);

        goodsService goodsService = new goodsService();
        int num;
        try {
            num = goodsService.selectGoodsNum(goodsID);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        goods goods = new goods();
        goods.setGoodsNum(num);
        String jsonString = JSON.toJSONString(goods);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
